package com.wang.gallerybackend.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.wang.gallerybackend.common.AjaxResult;
import com.wang.gallerybackend.common.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	@ExceptionHandler(NotLoginException.class)
	public BaseResponse<?> notLoginException(NotLoginException e) {
		log.error("NotLoginException", e);
		return AjaxResult.error(ErrorCode.NOT_LOGIN_ERROR, e.getMessage());
	}

	@ExceptionHandler(NotPermissionException.class)
	public BaseResponse<?> notPermissionExceptionHandler(NotPermissionException e) {
		log.error("NotPermissionException", e);
		return AjaxResult.error(ErrorCode.NOT_AUTH_ERROR, e.getMessage());
	}

	@ExceptionHandler(BusinessException.class)
	public BaseResponse<?> handleException(BusinessException e) {
		log.error("GlobalExceptionHandler:", e);
		return AjaxResult.error(e.getCode(),e.getMessage());
	}
	@ExceptionHandler(RuntimeException.class)
	public BaseResponse<?> handleException(RuntimeException e) {
		log.error("GlobalExceptionHandler:", e);
		return AjaxResult.error(ErrorCode.SYSTEM_ERROR,"系统错误");
	}
}
